#define _CRT_SECURE_NO_WARNINGS	

int Addnum(char* s, int x)
{
    int tmp = x;
    int count = 0;
    while (tmp)
    {
        count++;
        tmp /= 10;
    }
    int ret = count;
    tmp = 1;
    for (int i = 0; i < count - 1; i++)
    {
        tmp *= 10;
    }
    while (count)
    {
        *s = (char)(48 + x / tmp % 10);
        s++;
        tmp /= 10;
        count--;
    }
    return ret;
}

char* compressString(char* S)
{
    int len1 = strlen(S);
    char* str = (char*)calloc(3 * len1, 1);
    char* front, * end;
    front = end = S;
    int find = 1;
    int num = 0;
    int count = 0;
    while (*front != '\0')
    {
        if (find)
        {
            str[num] = *front;
            find = 0;
            count++;
            num++;
        }
        else
        {
            if (*front != *end)
            {
                end = front;
                int tmp = Addnum(&str[num], count);
                count = 1;
                num += tmp;
                str[num] = *front;
                num++;
            }
            else
            {
                count++;
            }
        }
        front++;
    }
    Addnum(&str[num], count);
    int len2 = strlen(str);
    return len1 > len2 ? str : S;
}